package com.tgpms.web.earlierStage.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import com.tgpms.auth.security.TokenService;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.util.Common;
import com.tgpms.web.earlierStage.entity.DocUploadEntityHistory;
import com.tgpms.web.earlierStage.service.IDocUploadEntityHistoryService;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 文档版本信息表 控制层
 * </p>
 *
 * @author newway
 * @since 2020-04-07
 */
@Api(value = "DocUploadEntityHistoryController", tags = "文档版本信息表相关接口")
@RestController
@RequestMapping("/ev/DocUploadEntityHistory")
@CrossOrigin//此注解解决可能的跨域问题
public class DocUploadEntityHistoryController {

	private final Logger log = LoggerFactory.getLogger(DocUploadEntityHistoryController.class);
	
	@Resource
	private IDocUploadEntityHistoryService service;
	@Resource
	private TokenService tokenService;
	
	@Value("${ftpparam.filepath3}")
	private String filepath3;
	@Value("${ftpparam.localloadfile}")
	private String localloadfile;
	
	@ApiOperation(value = "新增文档版本信息表接口",notes = "新增文档版本信息表接口",httpMethod = "POST")
	@PostMapping(value = "/addDocUploadEntityHistoryInfo",headers = "content-type=multipart/form-data")
	public Result addDocUploadEntityHistoryInfo(@RequestParam(required = false) String projectId,
			@RequestParam(required = false) String docId,
			@RequestParam(required = false) String docName,
			@RequestParam MultipartFile file,HttpServletRequest request) {
		log.info("======新增文档版本信息表接口接收到的参数为: {projectId : "+projectId+"}, {docId : "+docId+"},"
				+ " {docName : "+docName+"}");
		Result result = new Result();
		if(Common.isEmpty(projectId) || Common.isEmpty(docId)
				|| Common.isEmpty(docName) || Common.isEmpty(file)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try{
			DocUploadEntityHistory en = new DocUploadEntityHistory();
			en.setProjectId(projectId);
			en.setDocId(docId);
			//获取上传人
			String insertName = tokenService.getUserName(request);
			en.setUploadUser(insertName);
			en.setDocName(docName);
			result = service.addDocUploadEntityHistory(en,result,file);
		} catch (Exception e) {
			log.error("======新增文档版本信息表接口异常"+e);
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("增加异常");
			return result;
		}
		log.info("======新增文档版本信息表接口结束======");
		return result;
	}
	
	@ApiOperation(value = "删除文档版本信息表接口",notes = "删除文档版本信息表接口",httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteDocUploadEntityHistoryInfo")
	public Result deleteDocUploadEntityHistoryInfo(@RequestParam Serializable id,HttpServletRequest request) {
		log.info("======删除文档版本信息表接口接收到的参数为: {id: "+id+" }");
		Result result = new Result();
		try{
			result = service.deleteDocUploadEntityHistory(id,result);
		} catch (Exception e) {
			log.error("======删除文档版本信息表接口异常"+e);
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg("删除异常");
			return result;
		}
		log.info("======删除文档版本信息表接口结束======");
		return result;
	}
	
	
	@ApiOperation(value = "下载指定文档版本文件接口", notes = "下载指定文档版本文件接口", httpMethod = "POST")
	@PostMapping(value = "/downDocUpload")
	public ResponseEntity<byte[]> downDocUpload(@RequestParam(value = "id", required = true) String id,HttpServletRequest request) {
		ResponseEntity<byte[]> entity = null;
		InputStream in = null;
		try {
			entity = service.downHistoryDoc(id, in,entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
		}
		return entity;
	}
}
